package dk.flashnotes.fff.helpers.pageInspector {
	import dk.flashnotes.fff.FFFBase;
	import dk.flashnotes.fff.helpers.pageInspector.EColorCodes;
	import dk.flashnotes.fff.helpers.pageInspector.IClearable;
	import dk.flashnotes.fff.page.APage;
	import dk.flashnotes.fff.page.EPageStates;
	import dk.flashnotes.fff.stateHolder.StateHolderEvent;

	import flash.display.Sprite;
	import flash.events.Event;		
	/**
	 * @author jk
	 */
	public class PageLabel extends Sprite implements IClearable {

		private var _doLabel : InspectorLabel;
		private var _doPage : APage;

		private var _oColors : Object;

		public function PageLabel(doPage : APage) {
			_doPage = doPage;
			setup();
		}

		private function setup() : void {
			
			_oColors = new Object();
			_oColors[EPageStates.OPENING] = EColorCodes.uintYELLOW;
			_oColors[EPageStates.OPENED] = EColorCodes.uintGREEN;
			_oColors[EPageStates.CLOSING] = EColorCodes.uintYELLOW;
			_oColors[EPageStates.CLEARED] = EColorCodes.uintGREY;
			
			_doLabel = new InspectorLabel();
			addChild(_doLabel);
			
			updateLabel();
			_doLabel.setMarker(EColorCodes.uintGREY);
			
			setState(_doPage.sState);
			
			_doPage.addEventListener(StateHolderEvent.etSTATE_CHANGED, onPageStateChange, false, 50, true);
		}

		private function onPageStateChange(e : Event) : void {
			if (_doPage.sState != null) {
				setState(_doPage.sState);
			}
		}

		private function setState(sNewState : String) : void {
			if (_oColors[sNewState] != null) {
				_doLabel.setMarker(Number(_oColors[sNewState]));
			}
			updateLabel();
		}

		private function updateLabel() : void {
			_doLabel.sText = get_sLabelText();
		}

		private function get_sLabelText() : String {
			var sPageColor : String = "#FFFFFF";
			if (FFFBase.get_oInstance().get_cPageClassRefrenceFromAlias(_doPage.oTN.oTNData.sPageTypeClassAlias) == FFFBase.get_oInstance().oElementFactory.get_cDefaultPageType()) {
				sPageColor = "#F70000";
			}
			return "<font color='" + sPageColor + "'><b>" + _doPage.oTN.oTNData.sPageTypeClassAlias + "</b></font><font color='#DDDDDD'> : " + _doPage.sState + "</font>";
		}

		public function clear() : void {
			_doPage.removeEventListener(StateHolderEvent.etSTATE_CHANGED, onPageStateChange);
			_doLabel = null;
			_oColors = null;
			_doPage = null;
		}
	}
}
